const Mock = require('mockjs')

const tokens = {
  admin: {
    token: 'admin-token'
  }
}

const loginInfo = {
  'id': Mock.mock('@guid'),
  'name': Mock.mock('@name'),
  'username': 'admin',
  'password': '',
  'avatar': 'https://gw.alipayobjects.com/zos/rmsportal/jZUIxmJycoymBprLOUbT.png',
  'status': 1,
  'telephone': '',
  'lastLoginIp': '27.154.74.117',
  'lastLoginTime': 1534837621348,
  'creatorId': 'admin',
  'createTime': 1497160610259,
  'deleted': 0,
  'roleId': 'admin',
  'lang': 'zh-CN',
  'token': 'admin-token'
}

const users = {
  'admin-token': {
    id: '4291d7da9005377ec9aec4a71ea837f',
    name: 'admin',
    username: 'admin',
    password: '',
    avatar: '/avatar2.jpg',
    status: 1,
    telephone: '',
    lastLoginIp: '27.154.74.117',
    lastLoginTime: 1534837621348,
    creatorId: 'admin',
    createTime: 1497160610259,
    merchantCode: 'TLif2btpzg079h15bk',
    deleted: 0,
    roleId: 'admin',
    roles: ['admin']
  }
}

module.exports = [
  // user login
  {
    url: '/user/login',
    type: 'post',
    response: config => {
      const { username, password } = config.body
      const token = tokens[username]

      const usernameStand = ['admin', 'super'];
      // 强硬要求 ant.design 相同密码
      // '21232f297a57a5a743894a0e4a801fc3',
      const passwordStand = ['8914de686ab28dc22f30d3d8e107ff6c', '21232f297a57a5a743894a0e4a801fc3']; // admin, ant.design

      if (!usernameStand.includes(username) || !passwordStand.includes(password)) {
        return {
          code: 401,
          isLogin: true,
          message: '账户或密码错误.'
        }
      }

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: 'Account and password are incorrect.'
        }
      }

      return {
        code: 0,
        data: loginInfo,
        message: "success"
      }
    }
  },

  // get user info
  {
    url: '/user/info\.*',
    type: 'get',
    response: config => {
      const { token } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 0,
        data: info,
        message: "success"
      }
    }
  },

  // user logout
  {
    url: '/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 0,
        data: 'Login out success',
        message: "success"
      }
    }
  }
]
